package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.SystemDictionaryItem;
import cn.wolfcode.rbac.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.rbac.query.SystemDictionaryQueryObject;
import cn.wolfcode.rbac.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SystemDictionaryItemServiceImpl implements ISystemDictionaryItemService {
    @Autowired
    private SystemDictionaryItemMapper mapper;
    public void saveOrUpdate(SystemDictionaryItem u) {
        Integer sequence = u.getSequence();
        if(sequence==null){
            int i = mapper.selectMaxSequence(u.getParentId())+1;
            u.setSequence(Integer.valueOf(i));
        }
        if(u.getId()==null){
            mapper.insert(u);
        }else{
            mapper.updateByPrimaryKey(u);
        }
    }


    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }


    public PageInfo query(SystemDictionaryQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<SystemDictionaryItem> result = mapper.queryForList(qo);
        return new PageInfo(result);
    }


    public SystemDictionaryItem selectOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<SystemDictionaryItem> listAll() {
        return mapper.selectAll();
    }

    public List<SystemDictionaryItem> selectByParentSn(String sn) {
        return mapper.selectByParentSn(sn);
    }


}
